/*
 *  Copyright 2005 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.common.parsers.xml;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.io.IOException;
import java.io.Writer;

/**
 * A content handler that is used for plain text extracting. On instantiation
 * it is attached a <b>writer</b> that will accept the extracted text.
 *
 * @author Roman Puchkovskiy <a href="mailto:roman.puchkovskiy@blandware.com">
 * &lt;roman.puchkovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.2 $ $Date: 2005/07/26 14:02:00 $
 */
class TextExtractingContentHandler extends DefaultHandler {
    private Writer output;

    private TextExtractingContentHandler() {}

    TextExtractingContentHandler(Writer writer) {
        this.output = writer;
    }

    public void startElement(String uri, String localName, String qName,
                             Attributes attributes) throws SAXException {
        try {
            output.write(' ');
        } catch (IOException e) {
            throw new SAXException(e);
        }
    }

    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        try {
            output.write(' ');
        } catch (IOException e) {
            throw new SAXException(e);
        }
    }

    public void characters(char[] ch, int start, int length)
            throws SAXException {
        try {
            output.write(ch, start, length);
        } catch (IOException e) {
            throw new SAXException(e);
        }
    }

    public void ignorableWhitespace(char[] ch, int start, int length)
            throws SAXException {
        try {
            output.write(ch, start, length);
        } catch (IOException e) {
            throw new SAXException(e);
        }
    }

    public void skippedEntity(String name) throws SAXException {
        try {
            output.write(' ');
        } catch (IOException e) {
            throw new SAXException(e);
        }
    }

    public void processingInstruction(String target, String data)
            throws SAXException {
        try {
            output.write(' ');
        } catch (IOException e) {
            throw new SAXException(e);
        }
    }
}
